package com.hz.api.admin.web.app;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import com.hz.api.admin.common.Result.ResultInfo;
import com.hz.api.admin.model.enums.ArticleTags;
import com.hz.api.admin.model.request.SaveArticleRequest;
import com.hz.api.admin.model.request.UserLoginRequest;
import com.hz.api.admin.model.request.UserPasswordRequest;
import com.hz.api.admin.model.request.UserRegisterRequest;
import com.hz.api.admin.model.vo.ArticleListVO;
import com.hz.api.admin.model.vo.ArticleTagVO;
import com.hz.api.admin.web.service.ArticleService;
import com.hz.api.admin.web.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/user")
@Tag(name = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ArticleService articleService;

    @GetMapping("/password/list")
    @Operation(summary = "密码列表")
    public ResultInfo getUserPassword(@Parameter(name = "页大小")@RequestParam("pageSize") int pageSize,
                                      @Parameter(name = "页码")@RequestParam("pageNum") int pageNum,
                                      @RequestParam("searchKey") String searchKey){
        return ResultInfo.success(userService.getUserPassword(pageSize, pageNum, searchKey));
    }

    @PostMapping("/password/save")
    @Operation(summary = "密码保存")
    public ResultInfo saveUserPassword(@RequestBody @Validated UserPasswordRequest userPasswordRequest){
        userService.addUserPassword(userPasswordRequest);
        return ResultInfo.success();
    }

    @PostMapping("/password/delete")
    @Operation(summary = "密码删除")
    public ResultInfo deleteUserPassword(@RequestParam("ids") List<Long> ids){
        userService.deleteUserPassword(ids);
        return ResultInfo.success();
    }

    @PostMapping("/login")
    @Operation(summary = "登陆")
    public ResultInfo userLogin(@RequestBody UserLoginRequest userLoginRequest,HttpServletRequest request,
                                            HttpServletResponse response){
        return ResultInfo.success(userService.userLogin(userLoginRequest, request,response));
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public ResultInfo register(@RequestBody UserRegisterRequest userRegisterRequest,
                                HttpServletResponse response){
        userService.register(userRegisterRequest, response);
        return ResultInfo.success("注册成功，请等待审核");
    }

    @GetMapping("/token/refresh")
    @Operation(summary = "token刷新")
    public ResultInfo userTokenRefresh(HttpServletRequest request){
        Map<String, String> retMap = new HashMap<>();
        retMap.put("token", request.getHeader("token"));
        return ResultInfo.success(retMap);
    }

    @GetMapping("/menu")
    @Operation(summary = "目录")
    public ResultInfo getUserMenu(){
        return ResultInfo.success(userService.getUserMenu());
    }

    @GetMapping("/permission")
    public ResultInfo getPermission(){
        String[] permission = {"sys:user:add",
                "sys:user:edit",
                "sys:user:delete",
                "sys:user:import",
                "sys:user:export"};

        return ResultInfo.success(Arrays.stream(permission).toArray());
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户登陆信息")
    public ResultInfo getUserInfo(){
        return userService.getUserInfo();
    }

    @GetMapping("/article/tags")
    public ResultInfo getArticleTags(){
        List<ArticleTagVO> list = new ArrayList<>();
        for (ArticleTags value : ArticleTags.values()) {
            ArticleTagVO articleTagVO = new ArticleTagVO();
            articleTagVO.setTagName(value.tagName);
            articleTagVO.setTagValue(value.tagValue);
            list.add(articleTagVO);
        }
        return ResultInfo.buildSuccess(list);
    }

    @PostMapping("/article/save")
    public ResultInfo saveArticle(@RequestBody SaveArticleRequest request){
        articleService.saveArticle(request);
        return ResultInfo.success();
    }

    @GetMapping("/article/list")
    public ResultInfo articleList(@RequestParam("pageSize") int pageSize,
                                  @RequestParam("pageNum") int pageNum,
                                  @RequestParam("tags") String tags){
        List<Integer> tagList = new ArrayList<>();
        if(StringUtils.isNotBlank(tags) && !tags.equals("null")){
            String[] tagStr = tags.split(",");
            List<String> strList = Arrays.stream(tagStr).collect(Collectors.toList());
            tagList = strList.stream().map(Integer::valueOf).collect(Collectors.toList());
        }
        return ResultInfo.buildSuccess(articleService.articleList(pageSize, pageNum, tagList));
    }

    @GetMapping("/article/detail")
    public ResultInfo articleDetail(@RequestParam("articleId") Long articleId){
        ArticleListVO detail = articleService.getArticleDetail(articleId);
        return ResultInfo.buildSuccess(detail);
    }

    @GetMapping("/article/getShareToken")
    public ResultInfo getShareToken(@RequestParam("articleId") Long articleId){
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap();
        String token = Base64Encoder.encode(String.valueOf(articleId));
        return ResultInfo.buildSuccess(token);
    }

    @GetMapping("/article/share/detail")
    public ResultInfo articleShareDetail(@RequestParam("token") String token){
        Long articleId = Long.parseLong(new String(Base64Decoder.decode(String.valueOf(token)), Charset.defaultCharset()));
        ArticleListVO detail = articleService.getArticleDetail(articleId);
        return ResultInfo.buildSuccess(detail);
    }
}
